package com.ihomey.linkuphome.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ihomey.linkuphome.R;
import com.ihomey.linkuphome.listeners.OnItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

/**
 * Created by Administrator on 2016/12/28.
 */

public class LanguageListAdapter extends SwipeMenuAdapter<LanguageListAdapter.ListHolder> {
    public static final int TYPE_NORMAL = 1;
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_FOOTER = 2;
    //获取从Activity中传递过来每个item的数据集合
    private String[] languageArray;
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    //构造函数
    public LanguageListAdapter(String[] languageArray) {
        this.languageArray = languageArray;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        } else if (position == getItemCount() - 1) {
            return TYPE_FOOTER;
        } else {
            return TYPE_NORMAL;
        }
    }


    @Override
    public void onBindViewHolder(ListHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_NORMAL) {
            holder.nameText.setText(languageArray[position-1]);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(position);
                    }
                }
            });
            return;
        }else if (getItemViewType(position) == TYPE_FOOTER) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(position);
                    }
                }
            });
            return;
        }
        else {
            return;
        }
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_language_header, parent, false);
        } else if (viewType == TYPE_FOOTER) {
            return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_language_footer, parent, false);
        } else {
            return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_language, parent, false);
        }
    }

    @Override
    public ListHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ListHolder(realContentView);
    }

    //在这里面加载ListView中的每个item的布局
    class ListHolder extends RecyclerView.ViewHolder {
        TextView nameText;

        public ListHolder(View itemView) {
            super(itemView);
            nameText = (TextView) itemView.findViewById(R.id.tv_device_name);
        }
    }

    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView和FooterView
    @Override
    public int getItemCount() {
        return languageArray.length + 2;
    }

}
